Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
creando una función dentro de la función: el método "comprar" no está definido

Obtengo el método de error "la compra no está definida". ¿Qué está mal en el siguiente código? Por favor, ¿puede decirme dónde me estoy equivocando?

 var Portfolio = function() { let stockHoldings = new Map(); function buy(trade) { let tradesForTicker = stockHoldings.get(trade.ticker); if(tradesForTicker == null){ stockHoldings.set(trade.ticker,[trade]); }else{ tradesForTicker.push(trade); } } function sell(trade) { let ticker = trade.ticker; let tradesForTicker = stockHoldings.get(ticker); let precision = 5; if(tradesForTicker != null){ let quantityToSell = Number(Number(trade.quantity).toFixed(precision)); while(quantityToSell > 0) { quantityToSell = Number(Number(quantityToSell).toFixed(precision)); if(tradesForTicker.length > 0){ let nextTradeToSell = tradesForTicker[0]; if(nextTradeToSell.quantity == quantityToSell){ quantityToSell = 0; tradesForTicker.splice(0,1); } else if (nextTradeToSell.quantity < quantityToSell){ quantityToSell = quantityToSell - nextTradeToSell.quantity; tradesForTicker.splice(0,1); }else { nextTradeToSell.quantity = nextTradeToSell.quantity - quantityToSell; quantityToSell = 0; } } } if(tradesForTicker.length == 0){ stockHoldings.delete(ticker) } } } this.stockHoldings = function() { return stockHoldings(); } }; function generetePortfolio(tickers, actions, quantities, prices){ let portfolio = new Portfolio(); for(var i=0;i< tickers.length; i++){ let ticker = tickers[i].toString(); let action = actions[i].toString(); let quantity = quantities[i].toString(); let price = prices[i].toString(); let transactionTrade = new Trade(ticker,quantity,price,action); if(transactionTrade.isBuyOrDrip()){ portfolio.buy(transactionTrade); } if(transactionTrade.isSell()){ portfolio.sell(transactionTrade); } } return portfolio(); }

EDITAR más:

//Método agregado para generar cantidad total y precio promedio, PERO ESTO NO FUNCIONA

 function myPositions(tickers, actions, quantities, prices){ let portfolio = generetePortfolio(tickers, actions, quantities, prices); let returnArray = []; portfolio.stockHoldings.forEach((value, key) => { let totalQuantity = 0; let avgPrice = 0; let totalCost = 0; value.map( trade => { totalQuantity += trade.quantity ; totalCost += trade.quantity * trade.price; }); avgPrice = totalCost/totalQuantity; returnArray.push([key, totalQuantity,avgPrice]); } ) return returnArray; }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Está intentando utilizar las propiedades de su objeto de portfolio suponiendo que será un objeto basado en el constructor de Portfolio , pero no lo es.

Una función constructora implícitamente devuelve this . Ahora, el único lugar en el que ha usado this dentro de la función es adjuntarle una propiedad stockHoldings .

También debe adjuntar las otras propiedades.

Lo que sucede bajo el capó puede considerarse simplemente como: un objeto vacío local se crea implícitamente . Se devuelve al final implícitamente .

 var Portfolio = function () { // this = {}; (implicitly) let stockHoldings = new Map(); function buy(trade) { let tradesForTicker = stockHoldings.get(trade.ticker); if(tradesForTicker == null){ stockHoldings.set(trade.ticker,[trade]); }else{ tradesForTicker.push(trade); } } function sell(trade) { let ticker = trade.ticker; let tradesForTicker = stockHoldings.get(ticker); let precision = 5; if(tradesForTicker != null){ let quantityToSell = Number(Number(trade.quantity).toFixed(precision)); while(quantityToSell > 0) { quantityToSell = Number(Number(quantityToSell).toFixed(precision)); if(tradesForTicker.length > 0){ let nextTradeToSell = tradesForTicker[0]; if(nextTradeToSell.quantity == quantityToSell){ quantityToSell = 0; tradesForTicker.splice(0,1); } else if (nextTradeToSell.quantity < quantityToSell){ quantityToSell = quantityToSell - nextTradeToSell.quantity; tradesForTicker.splice(0,1); }else { nextTradeToSell.quantity = nextTradeToSell.quantity - quantityToSell; quantityToSell = 0; } } } if(tradesForTicker.length == 0){ stockHoldings.delete(ticker) } } } this.stockHoldings = stockHoldings; this.buy = buy; this.sell = sell; // return this; (implicitly) }; function generetePortfolio(tickers, actions, quantities, prices){ let portfolio = new Portfolio(); for(var i=0;i< tickers.length; i++){ let ticker = tickers[i].toString(); let action = actions[i].toString(); let quantity = quantities[i].toString(); let price = prices[i].toString(); let transactionTrade = new Trade(ticker,quantity,price,action); if(transactionTrade.isBuyOrDrip()){ portfolio.buy(transactionTrade); } if(transactionTrade.isSell()){ portfolio.sell(transactionTrade); } } return portfolio(); }

Además, la función para stockHoldings no parece correcta. Deberías devolver el mapa correctamente.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda